<template>
	<div>
		<el-dialog title="编辑药师" :visible.sync="pharEdit" class="editPerson">
		  <el-form ref="form" :model="form" status-icon :rules="rules" label-width="100px">
		  	<el-form-item label="登录账号" prop="user_name">
		  		<el-input v-model="form.user_name"></el-input>
		  	</el-form-item>
		  	<el-form-item label="同时修改密码">
		  		<el-switch v-model="form.delivery"></el-switch>
		  	</el-form-item>
			<el-form-item label="密码" v-if="form.delivery" prop="newPassword">
				<el-input type="password" v-model="form.newPassword"  autocomplete="off"></el-input>
			</el-form-item>
			<el-form-item label="确认密码" v-if="form.delivery" prop="rePassword">
				<el-input type="password" v-model="form.rePassword"  autocomplete="off"></el-input>
			</el-form-item>
		  	<el-form-item label="状态">
		  		<el-radio-group v-model="form.state">
		  		  <el-radio label="1">启用</el-radio>
		  		  <el-radio label="0">禁用</el-radio>
		  		</el-radio-group>
		  	</el-form-item>
		  	<el-form-item label="姓名">
		  		<el-input v-model="form.real_name"></el-input>
		  	</el-form-item>
		  	<el-form-item label="编号">
		  		<el-input v-model="form.user_no"></el-input>
		  	</el-form-item>
		  	<el-form-item label="部门">
		  		<el-input v-model="form.user_dept"></el-input>
		  	</el-form-item>
		  	<el-form-item label="备注">
		  		<el-input v-model="form.remark"></el-input>
		  	</el-form-item>
		  </el-form>
		  <div class="clear">
			  <el-button type="default" style="margin-bottom:10px;margin-left:10px;" class="fr" @click="hide">关闭</el-button>
		  	<el-button type="primary" style="margin-bottom:10px;" class="fr" @click="validateForm">保存</el-button>
			
		  </div>
		</el-dialog>
		
	</div>
</template>

<script>
	export default {
		name:"Editperson",
		data () {
			 var validateUn = (rule, value, callback) => {
				if (value === '') {
				  callback(new Error('请输入登录账号'));
				} else {
				  callback();
				}
			};
			var validatePass = (rule, value, callback) => {
				if(this.form.delivery){
					if (value === '') {
					  callback(new Error('请输入密码'));
					} else {
					  if (this.form.newPassword !== '') {
						this.$refs.form.validateField('rePassword');
					  }
					  callback();
					}
				}else{
					callback();
				}
			};
			var validatePass2 = (rule, value, callback) => {
				if(this.form.delivery){
					if (value === '') {
					  callback(new Error('请输入确认密码'));
					} else if (value !== this.form.newPassword) {
					  callback(new Error('两次输入密码不一致!'));
					} else {
					  callback();
					}
				}else{
					callback();
				}
			};
			return {
				pharEdit:false,
				id:"",
				form: {
					user_name: '',
					newPassword: '',
					rePassword: '',
					real_name: '',
					delivery: false,
					user_no: '',
					user_dept:'',
					state:'',
					remark:''
				},
				rules:{
					user_name: [
						{ validator: validateUn, trigger: 'blur' }
					],
					newPassword:[
						{ validator: validatePass, trigger: 'blur'}
					],
					rePassword:[
						{ validator: validatePass2, trigger: 'blur'}
					]
				}
			}	
		},
		props:{
			dataObj:{
				required: true,
				type:Object
			}
		},
		watch:{
			dataObj(val){
				this.id=this.dataObj.id;
				this.getDetail()
			}
		},
		methods:{
			async getDetail(){
				let res=await this.$ptc.pharEdit({
					id:this.id
				})
				
				if(res){
					this.form.user_name=res.user_name;
					this.form.real_name=res.real_name;
					this.form.user_no=res.user_no;
					this.form.user_dept=res.user_dept;
					this.form.state=(res.state).toString();
					this.form.remark=res.remark;
				}else{
					this.form.user_name=this.dataObj.user_name;
					this.form.real_name=this.dataObj.real_name;
					this.form.user_no=this.dataObj.user_no;
					this.form.user_dept=this.dataObj.user_dept;
					this.form.state=(this.dataObj.state).toString();
					this.form.remark=this.dataObj.remark;
				}
			},
			async sureFun(){
				var data={
					founder:0,
					id:this.id,
					user_name:this.form.user_name,
					newPassword:this.form.newPassword,
					state:this.form.state,
					real_name:this.form.real_name,
					user_no:this.form.user_no,
					user_dept:this.form.user_dept,
					remark:this.form.remark
				}
				if(this.form.delivery){
					data.updatePwd="yes";
				}
				var res=await this.$ptc.pharEditSave(data);
				this.$message({
				  message: res.msg,
				  type: res.code == 1?'success':'error',
				  duration:1500
				});
				if(res.code == 1){
					this.hide();
					this.$emit("distdepaChange")
				}
			},
			validateForm(){
				this.$refs.form.validate((valid) => {
				  if (valid) {
					this.sureFun();
				  } else {
					return false;
				  }
				});
			},
			show(){
				this.pharEdit=true;
			},
			hide(){
				this.pharEdit=false;
			}
		}
	}
</script>

<style>
</style>
